#include <cstdio>
#include <iostream>
#include "parserxml.h"

using namespace std;
using namespace tinyxml2;

namespace XMax {

    ParserXML::ParserXML() {
        m_xmlMap.clear();
    }

    void ParserXML::loadAndTraversalXml() {
        XMLDocument xmlFile;
        if(xmlFile.LoadFile(XML_FILE_PATH) != XML_SUCCESS) {
            perror("!!!Can't Open xml file\n");
            printf("Error ID = %d\n",xmlFile.ErrorID());
            return;
        } else {
            printf("Open xml file SUCESS\n");
        }
        if(xmlFile.RootElement() != NULL) {
            XMLElement* rootNode = xmlFile.RootElement();
            printf("rootNode name is - %s\n",rootNode->Name());
            if(rootNode->FirstChildElement() != NULL) {
                XMLElement* childNode = rootNode->FirstChildElement();
                std::string map_key;
                std::map<std::string,std::string> map_value_elem;
                std::vector<std::map<std::string,std::string> > map_value;
                for(;childNode != NULL;) {
                    printf("childNode NV is - %s - %s\n",childNode->Name(),childNode->FindAttribute("name")->Value());
                    map_key = childNode->FindAttribute("name")->Value();
                    if(childNode->FirstChild() != NULL) {
                        XMLNode* childNextNode = childNode->FirstChild();
                        for(;childNextNode != NULL;) {
                            printf("childNextNode NV - %s - %s\n",childNextNode->Value(),childNextNode->ToElement()->GetText());
                            map_value_elem.insert(std::pair<std::string,std::string>(childNextNode->Value(),childNextNode->ToElement()->GetText())) ; 
                            childNextNode = childNextNode->NextSibling();
                            if(childNode == NULL) {
                                printf("Traversal childNext end \n");
                            }
                        }
                        map_value.push_back(map_value_elem);
                    }
                    m_xmlMap.insert(std::pair<std::string,std::vector<std::map<std::string,std::string> > >(map_key,map_value));
                    map_value_elem.clear();
                    map_value.clear();
                    childNode = childNode->NextSiblingElement();
                    if(childNode == NULL) {
                        printf("Traversal child end \n");
                    }
                }
                
            }
        }
    }

    ParserXML::~ParserXML() {

    }

    void ParserXML::printXmlMap() {
        printf("XmlMap element count = %ld\n",m_xmlMap.size());
        for(auto key_it = m_xmlMap.begin(); key_it != m_xmlMap.end(); key_it++) {
            cout << "XmlMap key [" <<  key_it->first << "]" << endl;
            for(auto val_it =key_it->second.begin(); val_it != key_it->second.end(); val_it++) {
                for(auto vect_it = val_it->begin(); vect_it != val_it->end(); vect_it++) {
                    cout << "XmlMap Value [" <<  vect_it->first << "]" << "[" << vect_it->second << "]" << endl;
                }
            }
        }
    }
}
